package com.tanhua.dubbo.api;

import com.tanhua.domain.mongo.Visitors;
import com.tanhua.dubbo.api.mongo.VisitorApi;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@DubboService
public class VisitorApiImpl implements VisitorApi {

    @Autowired
    private MongoTemplate mongoTemplate;
    @Override
    public List<Visitors> queryVisitors(Long userId, int num) {
//        select * from visitors where userId=?? limit num
        Query query = new Query(Criteria.where("userId").is(userId));
        query.limit(num);
        query.with(Sort.by(Sort.Direction.DESC,"date"));
        return mongoTemplate.find(query,Visitors.class);
    }
}
